package com.clive.dao.impl;

import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.clive.bean.DishesCategory;
import com.clive.dao.DishesCategoryDao;
import com.clive.utils.DbcpUtils;

public class DishesCategoryDaoimpl implements DishesCategoryDao {

	public int addDishesCategory(DishesCategory dishesCategory) {
		Connection conn = DbcpUtils.getConnection();
		PreparedStatement pst = null;
		String sql = "INSERT INTO dishescategory(dishes_category_name,status,created,updated) VALUE(?,?,?,?)";
		try {
			pst = conn.prepareStatement(sql);
			pst.setString(1, dishesCategory.getDishesCategoryName());
			pst.setInt(2, dishesCategory.getStatus());
			pst.setDate(3, new Date(dishesCategory.getCreated().getTime()));
			pst.setDate(4, new Date(dishesCategory.getUpdated().getTime()));
			int i = pst.executeUpdate();
			return i;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			DbcpUtils.closeAll(conn, pst);
		}
		return 0;
	}

	public int deleteDishesCategory(Integer dishesCategoryId) {
		// TODO Auto-generated method stub
		return 0;
	}

	public int updateDishesCategory(DishesCategory dishesCategory) {
		// TODO Auto-generated method stub
		return 0;
	}

	public List<DishesCategory> findDishesCategoryAll() {
		Connection conn = DbcpUtils.getConnection();
		PreparedStatement pst = null;
		ResultSet result = null;
		String sql = "SELECT * FROM dishescategory WHERE status = 1";
		try {
			pst = conn.prepareStatement(sql);
			result = pst.executeQuery();
			List<DishesCategory> list = new ArrayList<DishesCategory>();
			while (result.next()) {
				DishesCategory category = new DishesCategory();
				category.setId(result.getInt("id"));
				category.setDishesCategoryName(result.getString("dishes_category_name"));
				category.setStatus(result.getInt("status"));
				category.setCreated(result.getDate("created"));
				category.setUpdated(result.getDate("updated"));
				list.add(category);
			}
			return list;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally {
			DbcpUtils.closeAll(conn, pst, result);
		}
		return null;
	}
}